Jump to content

[SOLVED] HELLLLLLP simple query problem MySQL


yozza84

Recommended Posts

Hi All,

Can someone please help me i have been searching all over the internet for ages now and still no one can help me even though it is simple.

 

<?php
include("header.php");
echo "<body>";
include("navbar.php");
include("bleftnav.php");
/*include("project1.php");*/
include ("searchbar.php");

//stuff goes here


//wildcard
   $stext = $_POST['text'];


$username="######";
$password="######";
$db_name="######";
$tbl_name="######";
$column="Name1";

mysql_connect("localhost", $username, $password)or die("cannot connect server ");
mysql_select_db("yojodb")or die("cannot select DB");

/*$query = sprintf("SELECT Name1, Phone FROM main WHERE Name1 LIKE $stext",
    mysql_real_escape_string($stext),
    mysql_real_escape_string($Phone));*/
   
$result = mysql_query("SELECT $column FROM $tbl_name WHERE $column LIKE $stext");

if (!$result) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $query;
    die($message);
}

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

echo "<div class='results'><p>Name : &nbsp {$row['Name']} </p></div> " ;
echo "<div class='results'><p>Phone : &nbsp {$row['Phone']} </p></div> " ;


    
}

if (isset($result))
{
echo "<div class='results'><p>Here are the Results, If your results do not appear you may need to refine your search.</p></div>";
}

echo $query;

mysql_free_result($result);

mysql_close()or die("cannot close connection");



include("footer.php");
?>

 

When we try to use the 'wildcard' we type in 's' for example and comes up with an error

 

Error:  "Invalid query: Unknown column 's' in 'where clause' Whole query:"

 

 

if anyone can help it will be much appreciated!!

been trying to figure it out for ages like whats wrong with me code, i tried loads of methods to use the wildcard i am willing to try anything so any suggestions welcome Smiley

Link to comment
Share on other sites

maybe i didnt explain it that well. :)

 

Please read the sql query i am using and see where the variable is, then see the error im getting.

 

SELECT Name1, Phone FROM main WHERE Name1 LIKE $stext      //This is the query

 

This is the error, i had this error when i typed 's' into the search bar on my website. As you can see the $stext is not meant to be the column, rather the string (or woteva)

 

Error:  "Invalid query: Unknown column 's' in 'where clause' Whole query:"

 

does anyone see what i mean? We've been having this problem for days now :(

 

Any help would be appreciated :)

 

 

Link to comment
Share on other sites

I gave you the solution. Put the $stext variable in quotes. If you don't, the query reads like

SELECT Name1, Phone FROM main WHERE Name1 LIKE s

 

Because there are no quotes around the s SQL assumes it is a column name.

 

 

PS If you aren't going to to take the advice when it's given, don't bother posting and wasting everyone's time.

 

 

 

 

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.