I think this goes here...
Ive been tinkering around, trying to make sense of the documentation and after getting 4 errors in a row ive given up with that...
Is there anyone who can give me a brief tutorial or has a nice link?
Offline
muppetds wrote:
I think this goes here...
Ive been tinkering around, trying to make sense of the documentation and after getting 4 errors in a row ive given up with that...
Is there anyone who can give me a brief tutorial or has a nice link?
Tutorial:
First you must connect to the mysql server (we'll use mysqli):
<?php $db = new mysqli($host, $username, $password, $db_name);
Next we make a query, take the result, and see if there were any errors.
$result = $db->query("SELECT * FROM `cows`") or die($db->error);
Finally, we print out the result:
while ($arr = $result->fetch_assoc) { echo "Next Row:\n<br />\n"; foreach ($arr as $k => $v) { echo $k . "=" . $v . "\n<br />\n"; } }
Offline
GeonoTRON2000 wrote:
muppetds wrote:
I think this goes here...
Ive been tinkering around, trying to make sense of the documentation and after getting 4 errors in a row ive given up with that...
Is there anyone who can give me a brief tutorial or has a nice link?Tutorial:
First you must connect to the mysql server (we'll use mysqli):Code:
<?php $db = new mysqli($host, $username, $password, $db_name);Next we make a query, take the result, and see if there were any errors.
Code:
$result = $db->query("SELECT * FROM `cows`") or die($db->error);Finally, we print out the result:
Code:
while ($arr = $result->fetch_assoc) { echo "Next Row:\n<br />\n"; foreach ($arr as $k => $v) { echo $k . "=" . $v . "\n<br />\n"; } }
ok thanks for your tutorial... Ill give it a try later
Offline
First of all I'd like to bring up a quote from a fellow programmer, Andrew Webster.
Keep going in programming and you will find that there are endless possibilities that are within your grasp. With a little research and time, you begin to realize that there really isn't any magic behind how computers and things work--and understanding even a little of that puts you miles ahead of the crowd.
That refers to your giving up after 4 tries... in my programming years I've encountered pesky errors that often stopped me in my tracks because I could find no way around them; I had to try many many times to get stuff right. Of course, most of the time it was a tiny little misplaced bracket, but that's another story
On to your problem, the solution offered by GeonoTRON is good and should work, but you need to have created a table with those values beforehand. Most people use phpMyAdmin as a tool for creating and managing MySQL data.
Also, more generally, I suggest taking a look through the W3 intro tutorial for MySQL. It's not the best way to learn in my opinion but it gives you a nice overview to get going.
Offline
LS97 wrote:
in my programming years I've encountered pesky errors that often stopped me in my tracks because I could find no way around them;
I've been working on a bug in my Android app on and off for three months now...
Offline
LS97 wrote:
First of all I'd like to bring up a quote from a fellow programmer, Andrew Webster.
Keep going in programming and you will find that there are endless possibilities that are within your grasp. With a little research and time, you begin to realize that there really isn't any magic behind how computers and things work--and understanding even a little of that puts you miles ahead of the crowd.
That refers to your giving up after 4 tries... in my programming years I've encountered pesky errors that often stopped me in my tracks because I could find no way around them; I had to try many many times to get stuff right. Of course, most of the time it was a tiny little misplaced bracket, but that's another story
On to your problem, the solution offered by GeonoTRON is good and should work, but you need to have created a table with those values beforehand. Most people use phpMyAdmin as a tool for creating and managing MySQL data.
Also, more generally, I suggest taking a look through the W3 intro tutorial for MySQL. It's not the best way to learn in my opinion but it gives you a nice overview to get going.
nice quote
Yea 4 tries = about 2 weeks per try....
Ok after i get apache on the VM working ill try the tutorial...
Last edited by muppetds (2013-04-10 06:54:02)
Offline
GeonoTRON2000 your code just shows a blank screen...
EDIT: putting a phpinfo() at the end shows the phpinfo... So im guessing the Query isn't working?
EDIT 2: i think i know whats causing it... let me check...
EDIT 3: Nope thats not it and its not echoing "Next Row:\n<br />\n" part... so im think you have made a mistake with the "while"
EDIT 4: doing echo $result shows nothing at all as well.. - not sure if this is related though
Last edited by muppetds (2013-04-10 13:31:19)
Offline
Ok ive done some testing and if EDIT 4 in the previous post is right then the mysqli Query isn't returning anything at all...
However it isn't actually showing any errors...
Last edited by muppetds (2013-04-11 03:19:35)
Offline
The problem is that the query is empty so there are no rows to loop through.
Try the W3 tutorial I told you about in my post and see if that helps.
Offline
LS97 wrote:
The problem is that the query is empty so there are no rows to loop through.
Try the W3 tutorial I told you about in my post and see if that helps.
ah ok. But shouldn't Select FROM test work? - There is a table called test..
Offline
muppetds wrote:
LS97 wrote:
The problem is that the query is empty so there are no rows to loop through.
Try the W3 tutorial I told you about in my post and see if that helps.ah ok. But shouldn't Select FROM test work? - There is a table called test..
Yeah, don't use tables you didn't create.
And yes it's select FROM, which is what's written in the script... what I'm not absolutely sure about is the while loop because I like to use for loops instead, but Geono probably tested it.
Offline
LS97 wrote:
muppetds wrote:
LS97 wrote:
The problem is that the query is empty so there are no rows to loop through.
Try the W3 tutorial I told you about in my post and see if that helps.ah ok. But shouldn't Select FROM test work? - There is a table called test..
Yeah, don't use tables you didn't create.
And yes it's select FROM, which is what's written in the script... what I'm not absolutely sure about is the while loop because I like to use for loops instead, but Geono probably tested it.
it is my own table inside my own database called test..
The table has some stuff in it well
Last edited by muppetds (2013-04-11 04:36:22)
Offline
muppetds wrote:
LS97 wrote:
muppetds wrote:
ah ok. But shouldn't Select FROM test work? - There is a table called test..Yeah, don't use tables you didn't create.
And yes it's select FROM, which is what's written in the script... what I'm not absolutely sure about is the while loop because I like to use for loops instead, but Geono probably tested it.it is my own table...
Oh ok. Usually the table test is already there when you get a server or install one on your computer.
Offline
Oops, sorry, here's what's wrong:
$result->fetch_assoc
should be
$result->fetch_assoc()
Offline
GeonoTRON2000 wrote:
Oops, sorry, here's what's wrong:
$result->fetch_assoc
should be
$result->fetch_assoc()
I didn't know query results were objects with actual functions
And I missed that...
Offline
Thanks to a steam freind ive found and fixed a few bugs
This is my code
<?php //set variables; $host = 'localhost'; $username = 'root'; $password = 'root'; $db_name = 'test'; //connect to database; $db = new mysqli($host, $username, $password, $db_name); // Check connection if (mysqli_connect_errno($con)) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } //Query Database; $result = $db->query("SELECT * FROM `test`"); //print result while($row = mysqli_fetch_array($result)); { echo $row['test']; echo "<br />"; } echo 'blah'; ?>
The database test and the table test do exist and are made by me... the table test has a collumn called test with 2 numbers inside it...
The page source im getting from the script is just this
<br />blah
Not good html i know but im more concerned with the fact its not showing anything to do with the table...
EDIT: The steam freind said add this in place of the while loop and it showed a blank scree with a blank source code.. (no Blah)
var_dump($res->fetch_array());
commenting it out shows blah though.. im confused now..
EDIT 2: my steam freind has ran the code and he got this
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in index.php on line 36
which is this part here
//print result while($row = mysqli_fetch_array($result));
He has said that the $result is 'false'
And doing this
//Query Database; $result = $db->query("SELECT * FROM test.test");
Works for him because he doesn't get the error anymore..
Im trying to work out how to get the errors here.. anybody know?
Last edited by muppetds (2013-04-11 16:28:44)
Offline
Ok after making a new database, with a new table and some random data inside it and doing a vardump of the SQL Query result, it shows that there is actually something there to echo out..
Offline
muppetds wrote:
Ok after spending more time with the steam freind we have got it to work
Glad to know it works!
Offline