Jump to content

Mysql Warning: mysql_fetch_array() expects parameter 1 to be resource,


adamriley

Recommended Posts

Hi could any one say why this is not working

 

-------------------------------------------------------------------------------------------------------

<?php

$username = "adam";

$password = "adamriley1996";

$hostname = "localhost";

 

//connection to the database

$dbhandle = mysql_connect($hostname, $username, $password)

or die("Unable to connect to MySQL");

echo "Connected to MySQL<br>";

 

//select a database to work with

$selected = mysql_select_db("examples",$dbhandle)

  or die("Could not select examples");

 

//execute the SQL query and return records

$result = mysql_query("SELECT id, model,year FROM cars");

 

//fetch tha data from the database

while ($row = mysql_fetch_array($result)) {

  echo "ID:".$row{'id'}." Name:".$row{'model'}."Year: ". //display the results

  $row{'year'}."<br>";

}

//close the connection

mysql_close($dbhandle);

?>

----------------------------------------------------------------------------------

error log

--------------------

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\test.php on line 19

ok fixed that one but what about this

 

<?php

$username = "adam";

$password = "adamriley1996";

$hostname = "localhost";

 

//connection to the database

$dbhandle = mysql_connect($hostname, $username, $password)

or die("Unable to connect to MySQL");

echo "Connected to MySQL<br>";

 

 

//select a database to work with

$selected = mysql_select_db("examples",$dbhandle)

  or die("Could not select examples");

$a1 = <<<my

INSERT INTO cars VALUES(1,'Mercedes','2000');

INSERT INTO cars VALUES(2,'BMW','2004');

INSERT INTO cars VALUES(3,'Audi','2001');

my;

mysql_query("$a1");

 

 

// execute the SQL query and return records

$result1 = mysql_query("SELECT id, model,year FROM cars");

 

// fetch that data from the database

while ($row = mysql_fetch_array($result1)) {

  echo "ID:".$row{'id'}." Name:".$row{'model'}."Year: ". //display the results

  $row{'year'}."<br>";

}

// close the connection

echo mysql_error();

mysql_close($dbhandle);

?>

 

------------

error log || nothing

---------------------------

You cannot execute multiple queries separated by semi-colons using the mysql_query() function. Too many 'programmers' were not validating external data to prevent sql injection so the msyql_query() function does not support multiple sql statements.

 

You either need to execute each query separately or you need to form a multi-value INSERT using a list of comma separated values.

 

INSERT statements that use VALUES syntax can insert multiple rows. To do this, include multiple lists of column values, each enclosed within parentheses and separated by commas. Example:

 

INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.