php/sql

  • Kurthead+1
  • Graduate
  • Graduate
  • Kurthead+1
  • Posts: 131

Post 3+ Months Ago

The tutorials don't explain quite enough. I'm able to connect to my hosting database, but I am unable to do the next couple of examples. The tutorials offer nothing for trouble shooting.

Code: [ Select ]
<?php
$con=mysqli_connect("localhost","user_name","*************");

// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL:" . mysqli_connect_error();
}
else
{
echo "successful connection";
}
// Create database
$sql="CREATE DATABASE my_db";
if (mysqli_query($con,$sql))
 {
 echo "Database my_db created successfully";
 }
else
 {
 echo "Error creating database: " . mysqli_error();
 }
?>
  1. <?php
  2. $con=mysqli_connect("localhost","user_name","*************");
  3. // Check connection
  4. if (mysqli_connect_errno($con))
  5. {
  6. echo "Failed to connect to MySQL:" . mysqli_connect_error();
  7. }
  8. else
  9. {
  10. echo "successful connection";
  11. }
  12. // Create database
  13. $sql="CREATE DATABASE my_db";
  14. if (mysqli_query($con,$sql))
  15.  {
  16.  echo "Database my_db created successfully";
  17.  }
  18. else
  19.  {
  20.  echo "Error creating database: " . mysqli_error();
  21.  }
  22. ?>


Also, on a slightly different topic, I'm curious why these functions execute, if they are only written as "if" conditions?
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • demonmaestro
  • Gold Member
  • Gold Member
  • User avatar
  • Posts: 679
  • Loc: Conroe, Texas

Post 3+ Months Ago

they are in a if statement to see if they exist. now if the database is there already then the if
Code: [ Select ]
14.$sql="CREATE DATABASE my_db";
and the if statement for it is not needed.
  • Kurthead+1
  • Graduate
  • Graduate
  • Kurthead+1
  • Posts: 131

Post 3+ Months Ago

Yes. The example only uses the if statement to make sure it's working. My biggest problem is though, that it won't create this database, and I can't figure out why. I have no trouble getting it to connect to mySQL on my host, but I can't get it to do anything after that.
  • demonmaestro
  • Gold Member
  • Gold Member
  • User avatar
  • Posts: 679
  • Loc: Conroe, Texas

Post 3+ Months Ago

on xampp if you go to localhost you should see phpmyadmin in it. use that to create your database.
  • Kurthead+1
  • Graduate
  • Graduate
  • Kurthead+1
  • Posts: 131

Post 3+ Months Ago

I'm actually using my hosting account site. But, I can't get it to create tables either. I need to be able to get stuff working, so I can learn how to do this php/sql stuff.
  • Kurthead+1
  • Graduate
  • Graduate
  • Kurthead+1
  • Posts: 131

Post 3+ Months Ago

Okay I can at least get it to put information into tables, so I guess I'll come back to this issue if it ever becomes relevant for me. Thanks guys.
  • demonmaestro
  • Gold Member
  • Gold Member
  • User avatar
  • Posts: 679
  • Loc: Conroe, Texas

Post 3+ Months Ago

any other questions please come back.
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6252
  • Loc: Seattle, WA

Post 3+ Months Ago

In the future, it would be helpful to include exactly how you're running your script and the error messages you're getting. We aren't mind-readers :)
  • Kurthead+1
  • Graduate
  • Graduate
  • Kurthead+1
  • Posts: 131

Post 3+ Months Ago

Good point lol.

Post Information

  • Total Posts in this topic: 9 posts
  • Users browsing this forum: No registered users and 81 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.