MySQL help!!

Status
Not open for further replies.

Mike

New member
Jun 27, 2006
6,777
116
0
51
On the firing line
I don't know what it is about MySQL, but it completely fucks with my head. All I want to do is pull some data from the database and display it on the page. Very simple. Yet, I've spent the past 2 hours with G**gle, Notepad++ and various PHP / MySQL sites and still can't get the required results.

Code:
$sql = mysql_query('SELECT title FROM musicvids WHERE artist LIKE "$artist"') or die (mysql_error());
while($row = mysql_fetch_array($sql))
{
    $row_array[] = $row['title'];
    echo $row['title'];
} 
echo "<br /><br />";

HELP!!
 


Code:
$sql = mysql_query("SELECT title FROM musicvids WHERE artist LIKE '{$artist}'") or die (mysql_error());

Just got your quoting backwards
 
  • Like
Reactions: Mike
If you are using LIKE then you should be using % as well, unless you are looking for exact matches. If you are looking for exact matches, you should move to = as they are much faster to process.
 
If you are using LIKE then you should be using % as well, unless you are looking for exact matches. If you are looking for exact matches, you should move to = as they are much faster to process.

Not sure I understand. Do you mean:
Code:
%$artist%
instead of
Code:
{$artist}
?
 
Not sure I understand. Do you mean:
Code:
%$artist%
instead of
Code:
{$artist}
?

The former.

% is mysql wild card for "a bunch of stuff" and is roughly equivalent to *
_ is mysql wild card for "one character" and is roughly equivalent to ?

"%acle" would match "miracle" and "pinnacle" but not "aclemitis"

like is generally only used with broad matches like wild card searches; otherwise it'll slow down your query. If you mean "equal to" use "=".

Like

"select * from artist where name = '$name'";
versus
"select * from artist where name like '%$name%'";

The wild cards can go on either side or both sides of the thing-to-match, and I'd use a statement like
"select * from artist where name like '%" .$name. "%'";
because I'd want to make sure that it was clear to both me and the computer that the wildcard wasn't part of the variable.
 
Code:
'%{$artist}%'

The {} have nothing to do with the mysql, they are just for properly embedding variables inside of strings(not needed but handy when you start working with objects so I just use them for everything now...and my zend cert test said you should lol).

The % stuff is explained by argh01 above.
 
Status
Not open for further replies.