Jump to content

Help on error: mysql_fetch_array() expects parameter 1 to be resource


SkilletZA

Recommended Posts

Hi Guys

 

Noob here.

 

Trying to browse MySql data through PHP script but getting this error:

 

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

 

 

This is the piece of code causing this:

 

<?php

$connect = mysqli_connect("192.168.0.3","demo","demo");
 
mysql_select_db("my_db");
 
$query = mysql_query("SELECT * FROM persons WHERE Age = '28' ");
 
WHILE($rows = mysql_fetch_array($query)):
 
$FirstName = $rows['FirstName'];
$LastName = $rows['LastName'];
$Age = $rows['Age'];
 
echo "$FirstName $LastName $Age";
 
endwhile;
 
?>
 
 
Pleas help!
 
 

He may not even understand what you mean by that since he's a self-proclaimed noob.
 
What jazzman1 is saying is that you are using mysqli_connect() to establish a database connection and you are using mysql_ functions for everything else.
 
mysqli_connect() is actually just an alias for the mysqli extension constructor and is all you need to make a db connection, you just need to define the database table inside the parameters like so:

$link = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db');

Like I said, mysqli_connect() is just an alias so people can use the procedural method which isn't recommended these days. Instead I'd use something like this:

<?php
$mysqli = new mysqli('192.168.0.3', 'demo', 'demo', 'my_db');
 
$result = $mysqli->query("SELECT * FROM persons WHERE Age = '28'");
while($rows = $result->fetch_assoc())
{ 
	$FirstName = $rows['FirstName'];
	$LastName = $rows['LastName'];
	$Age = $rows['Age'];
	 
	echo "$FirstName $LastName $Age";
}

Remember that you cannot use mysql_ functions with mysqli_functions.

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.