Im trying to make queries to get go to my DB and pull out everything that starts with the letter "A" in a row. I think I get it all except I dont know how to display the results. Heres what I got. $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql'); mysql_select_db($dbname); $sql = "SELECT * FROM babynames WHERE Name LIKE `a%`"; $result = mysql_query($sql); PHP: Just need the line to display the results. Thanks for looking and helping the stupid.
//Loop thru the rows in the result while($row = mysql_fetch_assoc($result)) { //Display the information you want echo $row['Names'] . "<br />\n"; } PHP: This will loop thru all the returned rows and print each name on a new line in your browser (the <br />). The \n will make the source more readable instead of throwing it all on the same line when you view source. You said something about including them all on the same row, I'm not sure what you meant, but if you want them all to show on the same row, replace the <br /> with a space or whatever characters you want to separate the names with.
I still get an error as you can see here http://www.www.dbseller.com/babynames/ Line 22 is the "while" line. This is the complete code that I am using. Does it look like I am missing something? <?php // This is an example of config.php $dbhost = 'localhost'; $dbuser = ''; $dbpass = ''; $dbname = ''; $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql'); mysql_select_db($dbname); $sql = "SELECT * FROM babynames WHERE Name LIKE `r%`"; $result = mysql_query($sql); while($row = mysql_fetch_assoc($result)) { echo $row['Names'] . "<br />\n"; } ?> PHP:
The error you are getting is telling you that $result isn't a valid resource. That means the MySQL query is probably generated an error. Run your MySQL query directly into MySQL and see what you get:
on line 11, add an || or die; to the end (remove the original and see if it ends before it errors out.
If you are saying make the line look like: $result = mysql_query($sql) || or die; then yes it does error out now.
sorry, take out the ||, its a double... you can use || ... you can also use "or" ... you can't use both (my bad).
Also, from mysql, run the following: describe babynames post the output here. The error you are getting definitely indicates something went wrong with the MySQL query, database or connection.
Not sure how to display the results of that but it displayed all 4 rows that I have. ID varchar(255) YES NULL Name varchar(255) YES NULL Meaning varchar(255) YES NULL Origin varchar(255) YES NULL
Look carefully at your $row['Names'] The database output reflects 'Name' Try something different for me:
I see you are back to the mysql resource error... This has to be something wrong with the MySQL stuff. Add "or die;" to the end of the mysql_select_db. Other than that, I'm just not seeing it. I have to run for the night. If you don't have it working by AM, PM me the entire code (leave out your passwords) and I'll setup a sample DB on my server and put in the code and find it for you.
Still nada. Good catch though. http://www.www.dbseller.com/babynames/ to see the errors I am getting.
Son of a case sensitive! All the names start with uppercase so thats why the lower case r wasnt returning nothing. All works now. Thanks
You could possibly prevent something like that to converting both to lowercase to compare them, maybe something like: SELECT * FROM babynames WHERE LOWER(Name) LIKE LOWER(a%)