Jump to content

[SOLVED] Trying to create search page Warning: mysql_fetch_array(): supplied argument is


farkewie

Recommended Posts

Hello i am trying to create a search page where i have a database with a table called "phones"

in that table i have columns;

id, make, model, userid , IMEI.

 

my form has i text box "id=search" form action "search.php" method "get".

 

i want to be able to use part word search but for now i am at least trying to get anything working.

 

so im trying to type motorola v6 in the text box and echo the make and model columns that match either both words or one or the other.

 

here is the error i cant seem to get past,

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource

 

here is my search.php

 

<?php 
error_reporting(E_ALL); 
ini_set('display_errors', true); 
include('Connections/mobiles.php'); ?>

<?php 
$search = $_GET['search'];

// testing to make sure i am getting details from search form ( working)
echo ("$search");


// connecting to the database
$connection  = mysql_connect($hostname_mobiles, $username_mobiles, $password_mobiles) or trigger_error(mysql_error(),E_USER_ERROR); 

//selecting the database
$db = mysql_select_db($database_mobiles);
// searching  table
$sql = "SELECT  FROM phones WHERE make LIKE '". $_GET['search']. "' AND model LIKE '". $_GET['search']. "' "; 

$phoneRow = mysql_fetch_array($sql);
$resultmake = $phoneRow['make'];
$resultmodel = $phoneRow['model'];
// display the phones
echo ("$resultmake");
echo ("$resultmodel");



?>

 

any tips at all would be great

Link to comment
Share on other sites

$sql = "SELECT  FROM phones WHERE make LIKE '". $_GET['search']. "' AND model LIKE '". $_GET['search']. "' ";

 

$phoneRow = mysql_fetch_array($sql);

 

That doesn't work $sql is a string u need to say something like this

 

$sql = "SELECT  FROM phones WHERE make LIKE '". $_GET['search']. "' AND model LIKE '". $_GET['search']. "' ";

$result = mysql_query($sql) or die(mysql_error());

$phoneRow = mysql_fetch_array($result);

 

 

Now if you have more than 1 row you need a loop to do it right.

Link to comment
Share on other sites

Thank you for your fast reply!!

 

i have changed like you said , im not sure how to loop im only new but ill google that when i get rid of these errors, im getting a new error now.

 

vYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM phones WHERE make LIKE 'Motorola v6' AND model LIKE 'Motorola v6'' at line 1

 

here is my new code:

 


<?php 
error_reporting(E_ALL); 
ini_set('display_errors', true); 
include('Connections/mobiles.php'); ?>

<?php 
$search = $_GET['search'];

// testing to make sure i am getting details from search form ( working)
echo ("$search");


// connecting to the database
$connection  = mysql_connect($hostname_mobiles, $username_mobiles, $password_mobiles) or trigger_error(mysql_error(),E_USER_ERROR); 

//selecting the database
$db = mysql_select_db($database_mobiles);
// searching  table
$sql = "SELECT  FROM phones WHERE make LIKE '". $_GET['search']. "' AND model LIKE '". $_GET['search']. "' "; 
$result = mysql_query($sql) or die(mysql_error());
   $phoneRow = mysql_fetch_array($result);
$resultmake = $phoneRow['make'];
$resultmodel = $phoneRow['model'];
// display the phones
echo ("$resultmake");
echo ("$resultmodel");



?>

 

i can never seem to get a query working when im going for multiple columns or tables,

Link to comment
Share on other sites

Thank you both for replies,

 

i am now getting another error

 

 

Use of undefined constant search - assumed 'search' in F:\www\xampp\htdocs\phone\test\test_result.php on line 7

 

herer is the whole code now

:

 

<?php 
error_reporting(E_ALL); 
ini_set('display_errors', true); 
include('Connections/mobiles.php'); ?>
<?php 
$search = mysql_real_escape_string($_GET[search]);

// testing to make sure i am getting details from search form ( working)
echo ("$search");


// connecting to the database
$connection  = mysql_connect($hostname_mobiles, $username_mobiles, $password_mobiles) or trigger_error(mysql_error(),E_USER_ERROR); 

//selecting the database
$db = mysql_select_db($database_mobiles);
// searching  table
$sql = "SELECT * FROM `phones` WHERE `make` LIKE '%$search%' OR `model` LIKE '%$search%'";
$result = mysql_query($sql) or die(mysql_error()); 

   $phoneRow = mysql_fetch_array($result);
$resultmake = $phoneRow['make'];
$resultmodel = $phoneRow['model'];
// display the phones
echo ("$resultmake");
echo ("$resultmodel");



?>

Link to comment
Share on other sites

Now its perfect thank you so much..

 

only other thing i am having trouble with is  i have access levels setup and "1" = User and "3" = Admin i amd trying to echo out what a user in the database is

 

i have never used "if" or "else" statments before this was my attempt. but it always just prints "adminuser" looks like its not checking and just printing both?

 

<?php 
$ul = $row_userinfo['level']; 

	if ($ul = ("1"));
{
	echo ("User");
}
	if ($ul = ("3"));
{
	echo ("Admin");
}
?>

Link to comment
Share on other sites

Sorry i am displaying the user information out of an existing database, ie; name, userid, workstream, level,

but i dont want the level displayed as a 1 or a 3 i want it too say admin or user ie

an admin will see there level as the work "admin" instead of "3"

 

i cant go hanging the database because my whole site checks for 1 or 3.

Link to comment
Share on other sites

ok everything is working great with

 

switch($row_userinfo['level']){
    case 1: $level = "User"; break;
    case 3: $level = "Admin"; break;
    default: $level = "User";
}

echo $level;

 

thank you all for your help i have learnt a lot of new stuff today

 

thanks...

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.