Mysql php error - quote system

I can not get my authors from my php quotes

I have a quote table: id, quote, aid

I have an author table: id, name, etc.

<?php

$DB_SERVER = "localhost";
$DB_USER = "root";
$DB_PASS = "";
$DB_NAME = "test";
$con = mysql_connect($DB_SERVER, $DB_USER, $DB_PASS);
mysql_select_db($DB_NAME);

$sql = mysql_query("SELECT * FROM quotes WHERE id = ".$_GET['id'], $con);
$row = mysql_fetch_row($sql);

$sql = mysql_query("SELECT * FROM author where aid = " . $row[1], $con);
$row = mysql_fetch_row($sql); 


var_dump($row);

now I get this error Warning: mysql_fetch_row () expects parameter 1 to be a resource, boolean is set in /var/www/domain.com/php.php on line 14 NULL

+5
source share
2 answers

if print_r($row);after the first request you see something like:

Array
(
    [0] => id
    [1] => quote 
    [2] => aid
)

then in the second request you use $row[1]which is a quote (string), not a number.

$sql = mysql_query("SELECT * FROM author where aid = " . $row[1], $con);

if you repeat the error (using mysql_error($con)), you will see something:

SQL; , MySQL

mysql_fetch_row mysql_fetch_assoc, . , . .

<?php
$_GET['id'] = 1;
$DB_SERVER = "localhost";
$DB_USER = "root";
$DB_PASS = "";
$DB_NAME = "test";
$con = mysql_connect($DB_SERVER, $DB_USER, $DB_PASS);
mysql_select_db($DB_NAME);
$sql = mysql_query("SELECT * FROM quotes WHERE id = " . (int)$_GET['id'], $con); // or you can use the mysql_real_escape_string
if(!$sql) {
  echo mysql_error($con);
}
$row = mysql_fetch_assoc($sql);
mysql_free_result($sql);

$sql = mysql_query("SELECT * FROM author where id = " . (int)$row['aid'], $con);
if(!$sql) {
  echo mysql_error($con);
}
$row = mysql_fetch_assoc($sql); 
mysql_free_result($sql);

print_r($row);
mysql_close($con);
+7

manual:

mysql_query() , FALSE .

mysql_query() FALSE, , .

,

$sql = mysql_query("SELECT * FROM author where aid = " . $row[1], $con);
if ( $sql ) {
    $row = mysql_fetch_row($sql); 
}
else {
    //error
}
+1

All Articles