Jump to content

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


SkilletZA
Go to solution Solved by 0xMatt,

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!
 
 
Link to comment
Share on other sites

  • Solution

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.