duanchoupo1104 2013-11-07 16:35
浏览 59

mysqli查询不起作用

I'm trying to learn more about MySQL databases and writing custom PHP scripts.

I've queried the WordPress database before and I'm comfortable adding custom tables, custom queries etc. But I've never created my own database before.

So far I've got a db called simplecms, with one table called core which has two columns, core_name and core_value. So far it has one row in it.

All I'm trying to do it echo out the value of this one row.

So far, I have this code:

[updated code]

<?php

// New Connection
$mysqli = new mysqli('localhost','root','root','simplecms');

// Check for errors
if( mysqli_connect_errno() ) {
    echo mysqli_connect_error();
} else {
    echo('connected to db...<br /><br />');
}

// Create Query
$query = "SELECT core_value FROM core WHERE core_name='url'";

// Execute Query
if( $result = $mysqli->query($query) ) {

    // Cycle through results
    while($row = $mysqli->fetch_object($result)){
        echo $row->column;
    }

    // Free result set
    $result->close();

} else {

    printf("Error message: %s
", $mysqli->error);

}

// Close connection
$mysqli->close();

?>

This returns: Error message: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '\'url\'' at line 1

Any ideas where I might be going wrong?

[update]

I've managed to get it working with some completely new code! I'm not 100% why this works, but here it is just incase:

<?php

// new Connection
$mysqli = new mysqli('localhost','root','root','simplecms');

// check for errors
if( mysqli_connect_errno() ) {
    echo mysqli_connect_error();
} else {
    echo('connected to db...<br /><br />');
}


// create a prepared statement
if($query = $mysqli->prepare("SELECT core_value FROM core WHERE core_name='url'")) {

    // execute
    $query -> execute();

    // bind results
    $query -> bind_result($result);

    // fetch value
    $query -> fetch();

    // echo out results
    echo $result;

    // close the statement
    $query -> close();

}

// close mysqli
$mysqli -> close();



?>
  • 写回答

2条回答 默认 最新

  • doue2666 2013-11-07 16:39
    关注
    SELECT core_value FROM core WHERE core_name=url
    

    From what you've said there is no url column so this query wont work. Use 'url'.

    In the future you'll be able to catch issues like this by examining $mysqli->error the documentation has examples.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度