This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2013-04-09 06:48:40

muppetds
Scratcher
Registered: 2011-02-11
Posts: 1000+

PHP and Mysql Queries...

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?


SCRATCH'S PARTLY INSANE RESIDENT 
http://internetometer.com/imagesmall/31691.pnghttp://bluetetrarpg.x10.mx/usercard/?name=muppetds

Offline

 

#2 2013-04-09 10:10:05

GeonoTRON2000
Scratcher
Registered: 2009-12-24
Posts: 1000+

Re: PHP and Mysql Queries...

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";
  }
}

http://i.imgur.com/BAEgGDL.png

Offline

 

#3 2013-04-09 11:29:15

muppetds
Scratcher
Registered: 2011-02-11
Posts: 1000+

Re: PHP and Mysql Queries...

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  wink


SCRATCH'S PARTLY INSANE RESIDENT 
http://internetometer.com/imagesmall/31691.pnghttp://bluetetrarpg.x10.mx/usercard/?name=muppetds

Offline

 

#4 2013-04-10 05:22:24

LS97
Scratcher
Registered: 2009-06-14
Posts: 1000+

Re: PHP and Mysql Queries...

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  tongue

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

 

#5 2013-04-10 06:12:38

blob8108
Scratcher
Registered: 2007-06-25
Posts: 1000+

Re: PHP and Mysql Queries...

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...  wink


Things I've made: kurt | scratchblocks2 | this cake

Offline

 

#6 2013-04-10 06:52:18

muppetds
Scratcher
Registered: 2011-02-11
Posts: 1000+

Re: PHP and Mysql Queries...

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  tongue

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  smile
Yea 4 tries = about 2 weeks per try....  hmm

Ok after i get apache on the VM working ill try the tutorial...

Last edited by muppetds (2013-04-10 06:54:02)


SCRATCH'S PARTLY INSANE RESIDENT 
http://internetometer.com/imagesmall/31691.pnghttp://bluetetrarpg.x10.mx/usercard/?name=muppetds

Offline

 

#7 2013-04-10 11:44:19

muppetds
Scratcher
Registered: 2011-02-11
Posts: 1000+

Re: PHP and Mysql Queries...

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)


SCRATCH'S PARTLY INSANE RESIDENT 
http://internetometer.com/imagesmall/31691.pnghttp://bluetetrarpg.x10.mx/usercard/?name=muppetds

Offline

 

#8 2013-04-11 03:11:25

muppetds
Scratcher
Registered: 2011-02-11
Posts: 1000+

Re: PHP and Mysql Queries...

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)


SCRATCH'S PARTLY INSANE RESIDENT 
http://internetometer.com/imagesmall/31691.pnghttp://bluetetrarpg.x10.mx/usercard/?name=muppetds

Offline

 

#9 2013-04-11 03:51:06

LS97
Scratcher
Registered: 2009-06-14
Posts: 1000+

Re: PHP and Mysql Queries...

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

 

#10 2013-04-11 04:22:43

muppetds
Scratcher
Registered: 2011-02-11
Posts: 1000+

Re: PHP and Mysql Queries...

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..


SCRATCH'S PARTLY INSANE RESIDENT 
http://internetometer.com/imagesmall/31691.pnghttp://bluetetrarpg.x10.mx/usercard/?name=muppetds

Offline

 

#11 2013-04-11 04:28:48

LS97
Scratcher
Registered: 2009-06-14
Posts: 1000+

Re: PHP and Mysql Queries...

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

 

#12 2013-04-11 04:31:28

muppetds
Scratcher
Registered: 2011-02-11
Posts: 1000+

Re: PHP and Mysql Queries...

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)


SCRATCH'S PARTLY INSANE RESIDENT 
http://internetometer.com/imagesmall/31691.pnghttp://bluetetrarpg.x10.mx/usercard/?name=muppetds

Offline

 

#13 2013-04-11 04:32:35

LS97
Scratcher
Registered: 2009-06-14
Posts: 1000+

Re: PHP and Mysql Queries...

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

 

#14 2013-04-11 10:22:43

GeonoTRON2000
Scratcher
Registered: 2009-12-24
Posts: 1000+

Re: PHP and Mysql Queries...

Oops, sorry, here's what's wrong:
$result->fetch_assoc
should be
$result->fetch_assoc()


http://i.imgur.com/BAEgGDL.png

Offline

 

#15 2013-04-11 10:40:53

LS97
Scratcher
Registered: 2009-06-14
Posts: 1000+

Re: PHP and Mysql Queries...

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  tongue
And I missed that...

Offline

 

#16 2013-04-11 14:40:00

muppetds
Scratcher
Registered: 2011-02-11
Posts: 1000+

Re: PHP and Mysql Queries...

Still doesn't work.. im going to add to it a bit and see if i can get it working...


SCRATCH'S PARTLY INSANE RESIDENT 
http://internetometer.com/imagesmall/31691.pnghttp://bluetetrarpg.x10.mx/usercard/?name=muppetds

Offline

 

#17 2013-04-11 15:35:41

muppetds
Scratcher
Registered: 2011-02-11
Posts: 1000+

Re: PHP and Mysql Queries...

Thanks to a steam freind ive found and fixed a few bugs
This is my code

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

Code:

<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)

Code:

 
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

Code:

//print result
while($row = mysqli_fetch_array($result));

He has said that the $result is 'false'

And doing this

Code:

  //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)


SCRATCH'S PARTLY INSANE RESIDENT 
http://internetometer.com/imagesmall/31691.pnghttp://bluetetrarpg.x10.mx/usercard/?name=muppetds

Offline

 

#18 2013-04-12 07:25:38

muppetds
Scratcher
Registered: 2011-02-11
Posts: 1000+

Re: PHP and Mysql Queries...

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..


SCRATCH'S PARTLY INSANE RESIDENT 
http://internetometer.com/imagesmall/31691.pnghttp://bluetetrarpg.x10.mx/usercard/?name=muppetds

Offline

 

#19 2013-04-12 09:33:40

muppetds
Scratcher
Registered: 2011-02-11
Posts: 1000+

Re: PHP and Mysql Queries...

Ok after spending more time with the steam freind we have got it to work  big_smile


SCRATCH'S PARTLY INSANE RESIDENT 
http://internetometer.com/imagesmall/31691.pnghttp://bluetetrarpg.x10.mx/usercard/?name=muppetds

Offline

 

#20 2013-04-12 14:32:30

LS97
Scratcher
Registered: 2009-06-14
Posts: 1000+

Re: PHP and Mysql Queries...

muppetds wrote:

Ok after spending more time with the steam freind we have got it to work  big_smile

Glad to know it works!

Offline

 

Board footer