dtwy2858 2013-03-18 23:29
浏览 27
已采纳

MAMP:不能做MySQL查询

Some background: I initially tried doing Apache, MySql, and PHP on windows, each installed separately (what a PITA that was!). Eventually I heard that I would be better suited on the mac, so I got the MAMP.

Glad to find that everything has been installed, I try playing around with it. Basically everything works, except for MySQL queries (I am trying to insert data into a table).

Here's my code:

<h1>hello world</h1>
<?php
echo "hello ";
echo date('H:i:s, jS F Y');
$link = mysql_connect('localhost', 'root', 'root');
if ($link)
    echo "success";
else
    echo "failure";

$db_selected = mysql_select_db('MyDB', $link);
if ($db_selected) 
    echo "success";
else
    echo "failure";

$my_query = mysql_query($link,"INSERT INTO comments (cName, cComment)
VALUES ('Peter', 'Hellloooooo')");

if ($my_query)
    echo "success";
else
    echo "failure";

mysql_close($link);
?>

My output looks like the following:

hello world

hello 00:14:12, 19th March 2013successsuccessfailure

As you can see, mysql_connect and mysql_select_db work, but mysql_query does not. I've spent many fruitless hours trying to figure this out. What is wrong with the code?

  • 写回答

1条回答 默认 最新

  • doumei1926 2013-03-18 23:32
    关注

    Try

    $my_query = mysql_query("INSERT INTO comments (cName, cComment)
    VALUES ('Peter', 'Hellloooooo')", $link);
    

    Next time better mysqli or PDO.

    Updated: Mysqli: U need to create mysqli connection

    $mysqli = mysqli_connect("host", "user", "password", "database");
    $stmt = $mysqli->prepare("INSERT INTO comments(cName, cComment) VALUES (?, ?)");
    
    $name = 'Peter';
    $comment = 'Haloooo';
    $stmt->bind_param("ss", $name, $comment);
    $stmt->execute();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试