Asked  9 Months ago    Answers:  5   Viewed   110 times

First off, I'm a total noob in mysql(i) language and know a little more than the basics of PHP.

Note: I do not manage or own / have access to the server on which the webpage currently is hosted. I can however access the phpMyAdmin page.

That said, I've got a webpage on which I am trying out some stuff. Right now I'm trying to make a log-in page linked to a database.

Now, behold the code:

$mysql_host = "localhost";
$mysql_user = "my_user";
$mysql_database = "my_database";
$mysql_password = "my_password";

$table = "my_tablename";

// Create connection 
$con=mysqli_connect($mysql_host,$mysql_user,$mysql_password,$mysql_database); 

// Check connection 
if (mysqli_connect_errno($con)) { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
}

// sending query
$result = mysqli_query("SELECT * FROM my_tablename");
if (!$result) {
    echo "Query to show fields from table failed! :-(";
}

Now, here comes the actual problem. As soon as I launch the page it will give me my "Query to show fields from table failed!" error message. But when I enter the same query on the phpMyAdmin 'SQL' tab, I get the wanted results. How come the webpage gives me an error, while the phpMyAdmin gives me the results, and, how do I solve it?

 Answers

68

Use correct syntax:

$result = mysqli_query($con, "SELECT * FROM my_tablename");

You forgot to link current mysqli connection. First parameter is link - which mysqli connection you want to use (good for multiple conns) and then the second is your query.

Wednesday, March 31, 2021
 
seaders
answered 9 Months ago
74

From the PHP Manual on mysqli_stmt::execute:

mysqli_stmt::execute -- mysqli_stmt_executeExecutes a prepared Query

Returns TRUE on success or FALSE on failure.


if ($stmt->execute()) { // exactly like this!
    $success = true;
}

You're doing it right... What's your dilemma?

Wednesday, March 31, 2021
 
Sauleil
answered 9 Months ago
70

Either set mysqli into Exception mode

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

or always check the result of every mysqli operation and throw mysqli error manually:

$result = $stmt->execute();
if (!$result) {
    throw new Exception($mysqli->error);
}

this is the only way to know what's wrong with your execute();

I have found that the SQL syntax apparently needs quotation marks around the fields for the VALUE

Of course it is wrong. SQL syntax apparently needs quotation marks around strings only.

Saturday, May 29, 2021
 
Eugenie
answered 7 Months ago
59

I ran into the same issue and found the following solution in the documentation: http://symfony.com/doc/current/book/testing.html#your-first-functional-test

To run your functional tests, the WebTestCase class bootstraps the kernel of your application. In most cases, this happens automatically. However, if your kernel is in a non-standard directory, you'll need to modify your phpunit.xml.dist file to set the KERNEL_DIR environment variable to the directory of your kernel:

<phpunit>
    <!-- ... -->
    <php>
        <server name="KERNEL_DIR" value="/path/to/your/app/" />
    </php>
    <!-- ... -->
</phpunit>

So check your phpunit.xml.dist configuration file and try to add the absolute path to your app-directory.

Hope it helps.

Friday, July 30, 2021
 
Bálint Molnár
answered 5 Months ago
88

Silly mistake on my part... simply forgot to add phpunit as a dependency in the project. For anyone else that gets this error, to composer.json add:

"require-dev": {
    "phpunit/phpunit": "3.7.*"
},

And then run:

composer update

That solved the problem.

Friday, September 17, 2021
 
Samir Sabri
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share