gamer-goddess Posted November 1, 2010 Share Posted November 1, 2010 ==> MySQL version 5.0.91-community So once upon a time I was working on a CMS. I worked on it for 3 months, had to wipe my computer - but didn't worry about it because I backed up the files on a flash drive... which got smushed by some boxes when I moved <.< awesome -.- Anyway, a year later (ie: today), I've decided to start again from scratch. For me - for whatever reason - the simplest things always seem to give me the most trouble. I decided to start with a "user" table - naturally - and began working on the first page: user_view.php and added an include file that connects to the database (connectDB.php). Since I started today, everything is simple - no design elements yet - just good 'ol code. The View Users Page is simply a page that grabs all the rows from Table: users and prints the information to the screen, as if you were viewing a Members page on a forum. This Page works fine. No complaints, does everything I need it to, prints all the rows and cols from Table Users. Then I created the Edit User Page. When I did this a year ago, and I wanted to print out all the data from the table, I used the while loop. When I wanted to only grab certain variables from a table, I just created a new set of variables, as show below. Using the same method from a year ago, I started working on printing various data to the screen (such as "Welcome $username"). 5 minutes of typing... refresh page... nada. So I stripped away just about every tag but the essentials and it still wouldn't work <?php ... $userID = 3; $sql = "SELECT * FROM users WHERE userID='$userID'"; $result = mysql_query($sql); $rows = mysql_fetch_row($result); echo $rows['username']; ... ?> Normally, this would just grab the username from a row in Table users where the userID = 3 But instead - it does nothing. I just see a blank page. It's a simple 5 lines of code and it's like "im just gonna do w.e the fk i wanna do mmmk thxforplaying" PS: Keep in mind there is nothing wrong with the connection to the database, and as already stated I'm able to print ALL values from the Users Table using this same method: <?php ... $user_sql = "SELECT * FROM users ORDER BY username ASC"; $user_result = mysql_query($user_sql); while ( $user_rows = mysql_fetch_array($user_result) ) ... ?> Quote Link to comment https://forums.phpfreaks.com/topic/217416-unable-to-display-single-row/ Share on other sites More sharing options...
trq Posted November 1, 2010 Share Posted November 1, 2010 mysql_fetch_row returns a numerically indexed array, not an associative array. Quote Link to comment https://forums.phpfreaks.com/topic/217416-unable-to-display-single-row/#findComment-1128893 Share on other sites More sharing options...
gamer-goddess Posted November 1, 2010 Author Share Posted November 1, 2010 Thanks for the reply, but a solution would be more beneficial Perhaps I'm not explaining myself properly. I've been trying different things. Previously mysql_fetch_row was written as mysql_fetch_array Neither work as of October 31 2010 One year ago, I distinctly remember typing 3 lines of code to achieve whatever I wanted to achieve. After googling to find the website that taught me this, I finally found it http://phpeasystep.com/mysql/6.html From http://phpeasystep.com/mysql/6.html ... // Select all columns from one row. "SELECT * FROM table_name WHERE column_name=' value in column '"; From http://phpeasystep.com/mysql/6.html If you don't want looping rows in mysql, replace while($rows=mysql_fetch_array($result)){ ........ with this $rows=mysql_fetch_array($result); Using this method, I SHOULD be able to type out $rows['colname'] ANYWHERE I feel like it. And yet, I'm unable to Keep in mind, I did this one year ago. 3 lines of code. Flawless Today - nada What gives? Quote Link to comment https://forums.phpfreaks.com/topic/217416-unable-to-display-single-row/#findComment-1129169 Share on other sites More sharing options...
Pikachu2000 Posted November 1, 2010 Share Posted November 1, 2010 <?php $userID = 3; $sql = "SELECT * FROM users WHERE userID='$userID'"; $result = mysql_query($sql) or die( 'Query: ' . $sql . '<br>Error: ' . mysql_error() ); $rows = mysql_fetch_assoc($result); echo $rows['username']; ?> Moving to PHP help . . . Quote Link to comment https://forums.phpfreaks.com/topic/217416-unable-to-display-single-row/#findComment-1129181 Share on other sites More sharing options...
gamer-goddess Posted November 1, 2010 Author Share Posted November 1, 2010 *sigh* I know the problem isn't with the php... or at least, I'm pretty sure it isn't, but if you think that will help, then sure Quote Link to comment https://forums.phpfreaks.com/topic/217416-unable-to-display-single-row/#findComment-1129188 Share on other sites More sharing options...
Andy-H Posted November 1, 2010 Share Posted November 1, 2010 I assume you're connecting to mysql and selecting your database before running this code? mysql_connect mysql_select_db Quote Link to comment https://forums.phpfreaks.com/topic/217416-unable-to-display-single-row/#findComment-1129190 Share on other sites More sharing options...
Andy-H Posted November 1, 2010 Share Posted November 1, 2010 <?php $host = 'localhost'; $user = 'root'; $pass = ''; $db = 'test'; $conn = mysql_connect($host, $user, $pass) or die('Failed to connect to mysql'); mysql_select_db($db, $conn); //also ensure mysql user is attached to correct database with correct privs $userID = 3; $sql = "SELECT * FROM users WHERE userID= " . (int)$userID; $result = mysql_query($sql) or die( 'Query: ' . $sql . '<br>Error: ' . mysql_error() ); $rows = mysql_fetch_assoc($result); echo '<pre>' . print_r($rows, true) . '</pre>'; //echo $rows['username']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/217416-unable-to-display-single-row/#findComment-1129195 Share on other sites More sharing options...
gamer-goddess Posted November 4, 2010 Author Share Posted November 4, 2010 nevermind figured it out My original code works fine, and I thought I was going crazy. The problem wasn't with the php, it was with the actual table in the database. So WITHOUT changing ANY syntax, I deleted the table in the database and re-wrote it with the exact row names as before and viola~! It worked So something was in fact wrong, but it wasn't with misspelling I must have set a row type to something weird and didn't realize it, so I just dropped the table altogether CONSIDER THIS PUZZLE SOLVED Quote Link to comment https://forums.phpfreaks.com/topic/217416-unable-to-display-single-row/#findComment-1130312 Share on other sites More sharing options...
PFMaBiSmAd Posted November 4, 2010 Share Posted November 4, 2010 You should always have error checking and error reporting logic in your php code so that you will get an indication of when queries fail and you can use msyql_error() to find out why they are failing. Once you check if a query executed without any errors, you should check if a query matched any rows before attempting to fetch any data from those rows. Quote Link to comment https://forums.phpfreaks.com/topic/217416-unable-to-display-single-row/#findComment-1130324 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.