hayes121 Posted April 14, 2014 Share Posted April 14, 2014 Hi guys i am new here and new to mysql, I am having some trouble reading data from my table. When ever i run my code i get the following error: Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /Applications/XAMPP/xamppfiles/htdocs/lab9.php on line 8 How can i fix this i have tried all that i know and nothing is working, here is the code i am using Any help will be greatly appricated! <html><head><title></title><?php$con = mysqli_connect("localhost", "root", "", "testTable");echo '<table border="1">'."\n";$result = mysqli_query($con, "SELECT * FROM table");while ( $row = mysqli_fetch_array($result)) { echo("</tr><td>"); echo(htmlentities($row[0])); echo("</td><td>"); echo(htmlentities($row[1])); echo("</td><td>"); echo(htmlentities($row[2])); echo("</td><td>"); echo('<a href="edit.php?id=' .htmlentities($row[0]).' ">Edit</a> /'); echo('<a href="delete.php?id=' .htmlentities($row[0]).' ">Delete</a> '); echo("</td></tr>\n"); }mysqli_close($con); ?> Quote Link to comment Share on other sites More sharing options...
Barand Posted April 14, 2014 Share Posted April 14, 2014 If "table" really is the name of your table I suggest you change it as "table" is a reserved word in SQL. If you must use that name you have to put it in backticks "SELECT * FROM `table` " and avoid using "SELECT *", specify the columns you want to select Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 14, 2014 Share Posted April 14, 2014 I would also suggest using the named indexes of the value from the database instead of 0, 1, 2, etc. You will save yourself a ton of problems when you have to modify/debug the code. Quote Link to comment Share on other sites More sharing options...
hayes121 Posted April 14, 2014 Author Share Posted April 14, 2014 Thanks for the feedback guys!!! When you say the 'named indexes' does that mean the column names that i have set in the table myself? Also another question with regards php and databases. I want to add a login in function to the site, I have all the html code for the site done and the php login file, but do i need to combine the two files to a .html file or a .php file? Or do i leave them seperate and reference the .php login from the html? Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 14, 2014 Share Posted April 14, 2014 When you say the 'named indexes' does that mean the column names that i have set in the table myself? Yes I want to add a login in function to the site, I have all the html code for the site done and the php login file, but do i need to combine the two files to a .html file or a .php file? Or do i leave them seperate and reference the .php login from the html? Typically you will want all of your pages to be PHP. Any page that is requested from the user would need to perform a check to see if the user is logged in. If yes, display the requested content. If no, redirect to the login page. If you have any pure HTML pages and the user can type in the URL to that page you can't restrict them via any login functionality. Well, technically you can, but it gets complicated and not worth discussing at this time. Quote Link to comment Share on other sites More sharing options...
hayes121 Posted April 14, 2014 Author Share Posted April 14, 2014 sorry for all the questions and thanks a million for the help! Is there a way to convert html to php? or do i just need to change the tags? for example i have attached my 'parts' page (.html) and then some simple php code to print out a table from my data base. Would you be able to join the two of these for me and re upload it? Login.html test.php Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 14, 2014 Share Posted April 14, 2014 Is there a way to convert html to php? Change the file name from somefile.htm to somefile.php and it's a PHP file. Now, it will still just output whatever you had before and you would still need to implement some code to initiate the check for login though. Besides those two files won't do anything just by putting them together. FYI: many of us regulars get a little peeved by people posting homework on here and trying to get people to do it for them. I find it unlikely your DB is named "Assignment" for any other reason. Would you be able to join the two of these for me and re upload it? No. If you want someone to do this for you, look at posting in the freelance forum. Although I doubt anyone would be willing to do it for free. Quote Link to comment Share on other sites More sharing options...
hayes121 Posted April 15, 2014 Author Share Posted April 15, 2014 sorry i didnt mean to offend in asking you to do it for me was just looking to see how it is done and to have an example, really appricate all the help you have given me though! Thanks Quote Link to comment Share on other sites More sharing options...
hayes121 Posted April 15, 2014 Author Share Posted April 15, 2014 Last question I promise I have set up a seperate table in my data base called 'my_build', I am trying to insert new data into this table . I have written up what i though was the write code but i doesnt seem to be adding the data to the table, can you see any problems with the code? <?php$con = mysqli_connect("localhost", "root", "", "Computers");if ( isset($_POST['serial_no']) && isset($_POST['description']) && isset($_POST['price']) && isset($_POST['fname']) && isset($_POST['lname']) ) { $s = mysql_real_escape_string($_POST['serial']); $d = mysql_real_escape_string($_POST['description']); $p = mysql_real_escape_string($_POST['price']); //$sql = "INSERT INTO `my_build` (Serial_no, Description, Price) //VALUES ('$s', '$d', '$p')"; echo "<pre>\n$sql\n</pre>\n"; mysqli_query($con, "INSTER INTO `my_build` (serial_no, description, price) VALUES ('$s', '$d', '$p')"); echo 'Success - <a href="home.php">Continue...</a>'; return; }mysqli_close($con); ?><p>Add A New User</p><form method="post"><p>Serial_no:<input type="text" name="serial_no"></p><p>Description:<input type="text" name="description"></p><p>Price:<input type="text" name="price"></p><p><input type="submit" value="Add New"/><a href="home.php">Cancel</a></p></form> Quote Link to comment Share on other sites More sharing options...
Barand Posted April 15, 2014 Share Posted April 15, 2014 (edited) mysqli_query($con, "INSTER INTO `my_build` (serial_no, description, price) VALUES ('$s', '$d', '$p')"); Use echo $con->error; to check for errors Edited April 15, 2014 by Barand Quote Link to comment Share on other sites More sharing options...
hayes121 Posted April 15, 2014 Author Share Posted April 15, 2014 Thank you !! Quote Link to comment 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.