1

ok so I have the following code that I am running in the python shell:

import MySQLdb
db = MySQLdb.connect(host = "xxxx",user="xxxx"password="xxxx",db="xxxx")
cur = db.cursor()
cur.execute(CREATE TABLE qqqq (asdf VARCHAR(20),fdsa VARCHAR(20))

Fairly sure the connection part is working, I'll get an error if I enter in the wrong value, or if I deny access to the database for my computer's IP address.

on the webhosting server, I have the following basic index.php file, which I have tested on a server on my computer, and I know works. when I go to the website domain, I get the following error: "Database query failed."

Any ideas why the MySQL query isn't working? My webhosting is Cpanel with godaddy.com, should I look for something else?

<?php
$dbhost = "xxxx";
$dbuser = "xxxx";
$dbpass = "xxxx";
$dbname = "xxxx";
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname); /*1*/

if(mysqli_connect_errno()) {
    die("Database connection failed: " .
        mysqli_connect_error() .
        " (" . mysqli_connect_errno() . ")"
        );
    }

?> 
<?php
 $query ="SELECT * FROM qqqq"; /*2*/
 $result = mysqli_query($connection, $query);
 if (!$result) {
die("Database query failed.");
 } 

?>

<!DOCTYPE html PUBLIC >

<html lang="en">
<head>
    <title></title>
</head>
<body>

<ul>
<?php /*3*/
while($subject = mysqli_fetch_assoc($result)){
    ?><li><?php echo $subject['asdf'];?></li>
<?php
}
?>
</ul>
<?php
    mysqli_free_result($result); /*4*/
?>

</body>
</html>

<?php 
mysqli_close($connection); /*5*/
?>
2
  • What happens when you run it? Commented Jun 17, 2014 at 0:36
  • nothing in particular. Is there a way to get the response from the database? Commented Jun 17, 2014 at 0:50

3 Answers 3

1

You should call db.commit() to have it complete. By default, autocommit is turned off.

You also have an error in your code. The SQL should be a string.

Sign up to request clarification or add additional context in comments.

3 Comments

yes that works, nvm, I was being stupid and pasting in multiple lines at the same time.
Awesome! Could you please accept this as the answer?
as soon as i figure out how
0

Shouldn't the cursor execute be calling a string? You don't have quotes around your sql statement.

1 Comment

thanks, I must have just copied it wrong, I use quotes in the actual python shell.
0

This line without quotas is incorrect:

cur.execute(CREATE TABLE qqqq (asdf VARCHAR(20),fdsa VARCHAR(20))

It should be

cur.execute("CREATE TABLE qqqq (asdf VARCHAR(20),fdsa VARCHAR(20))")

So test in your database whether you really have table qqqq.
You could install SQL Buddy or phpMyAdmin.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.